from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re


from selenium.webdriver.common.keys import Keys

class twitter:
    def addPost(self, proxy, account, post):
        try:
            if(proxy == True):

                PROXY_HOST = "127.0.0.1"
                PROXY_PORT = 8118

                fp = webdriver.FirefoxProfile()

                # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
                fp.set_preference("network.proxy.type", 1)

                fp.set_preference("network.proxy.http", PROXY_HOST)
                fp.set_preference("network.proxy.http_port", PROXY_PORT)
                fp.set_preference("network.proxy.ftp", PROXY_HOST)
                fp.set_preference("network.proxy.ftp_port", PROXY_PORT)
                fp.set_preference("network.proxy.ssl", PROXY_HOST)
                fp.set_preference("network.proxy.ssl_port", PROXY_PORT)

                fp.set_preference("network.proxy.no_proxies_on", "") # set this value as desired

                #driver = webdriver.Firefox(firefox_profile=fp)



                driver = webdriver.Firefox(firefox_profile=fp) # Get local session of firefox
            else:
                driver = webdriver.Firefox()


            #twitter no mobile
#            driver.implicitly_wait(10)
#            base_url = "http://twitter.com/"
#
#            driver.get(base_url)
#            driver.find_element_by_xpath("//div[@id='front-container']/div[2]/div/form/fieldset/div/input").clear()
#            driver.find_element_by_xpath("//div[@id='front-container']/div[2]/div/form/fieldset/div/input").send_keys(account[0])
#            driver.find_element_by_xpath("//div[@id='front-container']/div[2]/div/form/fieldset/div[2]/input").clear()
#            driver.find_element_by_xpath("//div[@id='front-container']/div[2]/div/form/fieldset/div[2]/input").send_keys(account[1]+Keys.RETURN)
#            time.sleep(25)
#            driver.find_element_by_css_selector("#new-tweet > span").click()
#            time.sleep(2)
#            driver.find_element_by_css_selector("div.twttr-dialog-content > div.tweet-box > div.text-area > div.text-area-editor.twttr-editor > textarea.twitter-anywhere-tweet-box-editor").clear()
#            driver.find_element_by_css_selector("div.twttr-dialog-content > div.tweet-box > div.text-area > div.text-area-editor.twttr-editor > textarea.twitter-anywhere-tweet-box-editor").send_keys(post)
#            driver.find_element_by_xpath("//div[@id='tweet_dialog']/div/div[2]/div[2]/div/div/div[3]/div[2]/a").click()
#            time.sleep(15)
            #print driver
            driver.implicitly_wait(30)
            driver.get("https://mobile.twitter.com/session/new")
            time.sleep(5)
            driver.find_element_by_id("username").clear()
            driver.find_element_by_id("username").send_keys(account[0])
            driver.find_element_by_id("password").clear()
            driver.find_element_by_id("password").send_keys(account[1])
            driver.find_element_by_id("signupbutton").click()
            time.sleep(7)
            driver.find_element_by_id("tweet_text").clear()
            driver.find_element_by_id("tweet_text").send_keys(post)
            driver.find_element_by_id("tweet_submit").click()
            time.sleep(5)

            driver.close()
            return True
        
        except :
            
            driver.close()
            return False


